package com.haiyi.software.app.basic;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.entity.BeanLoginResultBody;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

public abstract class ActivityBasicGiftListData extends ActivityBasic {

	private final String TAG = "ActivityBasicGiftListData";

	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	protected List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

	protected String str_server = null;

	private String str_requestName = null;

	protected String userUID = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
	}

	/**
	 * 设置请求的地址
	 * 
	 * @param str_requestName
	 */
	protected void setStr_requestName(String str_requestName) {
		this.str_requestName = str_requestName;
	}

	@Override
	public synchronized void requestNetworkData(Object... args) {

		str_server = this.getResources().getString(R.string.URL);

		String str_requestURL = str_server + str_requestName;

		AsyncHttpClient client = new AsyncHttpClient();

		client.post(str_requestURL, responseHandler);
	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			try {
				String str_Return = new String(responseBody, "UTF-8");

				Log.i(TAG, "礼物返回值" + str_Return);
				JsonParser parser = new JsonParser();

				JsonObject jsonReturn = parser.parse(str_Return)
						.getAsJsonObject();

				if (jsonReturn.get("state").getAsString().equals("200")) {

					putDateToList(jsonReturn.get("body").getAsJsonObject()
							.get("personlist").getAsJsonArray());

					Log.i(TAG, "数据加载完毕");

					data.clear();
					data.addAll(list);

					requestFinish(null);

				}

			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {
			// TODO 自动生成的方法存根

		}
	};

	private void putDateToList(JsonArray asJsonArray) {
		// TODO 自动生成的方法存根

		Gson gson = new Gson();

		Type type = new TypeToken<Map<String, String>>() {
		}.getType();

		list.clear();

		for (JsonElement jsonElement : asJsonArray) {

			Map<String, Object> map = gson.fromJson(jsonElement, type);

			list.add(map);
		}
	}

}
